package com.peach.utils;
import com.notnoop.apns.APNS;
import com.notnoop.apns.ApnsService;
import com.peach.controller.DeviceManage.MDMController;
import com.peach.domain.DeviceManage.Device;
import com.peach.domain.UserManage.CurrentUser;
import com.peach.service.DeviceManage.DeviceApplication;
import com.peach.service.DeviceManage.PushMsgApplication;
import com.peach.service.UserManage.UserApplication;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Configuration
@EnableScheduling
public class SchedulingConfig {
    @Autowired
    private UserApplication userApplication;

    @Autowired
    private DeviceApplication deviceApplication;

    @Autowired
    private PushMsgApplication pushMsgApplication;

    ApnsService service = APNS.newService().withCert(MDMController.certPath,MDMController.certPassword).withProductionDestination().build();

    @Scheduled(cron = "*/10 * * * * *")
    public void checkCurrentUserLife(){//查询数据库中当前预下载账户
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CurrentUser curUser= userApplication.selectCurrentUser();
        if (curUser == null){

        }else{
            try {
                Date now = new Date();
                System.out.println("当前时间为："+sdf.format(now));
                Date downloadTime = sdf.parse(curUser.getPreDownloadTime());
                System.out.println("下载的时间为："+sdf.format(downloadTime));
                long distance = now.getTime() - downloadTime.getTime();
                if (distance >= 5*60*1000){//如果相差10分钟或者以上则删除，控制获取描述文件后一分钟内必须安装好。
                    int affRows = userApplication.deleteCurrentUser();
                }
            }catch (Exception e){
                System.out.println("scheduled error :"+e.getMessage());
            }
        }
    }
//    @Scheduled(cron = "0 0/5 * * * ? ")
    public void updateDeviceDetail(){
        List<Device> devices = deviceApplication.selectAllDevice();
        if (devices == null){
        }else{
            for (Device device: devices
                 ) {
                System.out.println("定时更新设备状态执行");
                MDMController.msg = pushMsgApplication.prepareQueryPlist(device.getUdid());
                String token = null;
                try {
                    token = TokenConfig.parseToken(device.getToken());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String pushMagic = device.getPushMagic();
                String payload = APNS.newPayload().customField("mdm",pushMagic).build();
                service.push(token,payload);
            }
        }
    }
//    @Scheduled(cron = "0 0/17 * * * *")
    public void updateDeviceApp(){
        List<Device> devices = deviceApplication.selectAllDevice();
        if (devices == null){
        }else{
            for (Device device: devices
                    ) {
                String token = null;
                try {
                    token = TokenConfig.parseToken(device.getToken());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String pushMagic = device.getPushMagic();
                String payload = APNS.newPayload().customField("mdm",pushMagic).build();
                service.push(token,payload);
                MDMController.msg = pushMsgApplication.prepareQueryInstalledAppPlist(device.getUdid());
            }
        }
    }
}
